package com.itheima.dao;

import com.itheima.bean.pojo.User;
import com.itheima.bean.vo.AddUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {

    /**
     * 添加用户数据
     * @param user
     * @return
     */
    @Insert("insert into t_user(username,password,remark,email,createTime) " +
            "values (#{username},#{password},#{remark},#{email},now())")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    public int addUser(AddUser user);

    /**
     * 添加用户角色
     * @param userid
     * @param roleid
     * @return
     */
    @Insert("insert into t_user_role(user_id, role_id) values (#{userid},#{roleid})")
    public int addUserRole(@Param("userid") Integer userid,
                           @Param("roleid") Integer roleid);



    /**
     * 分页查询： 用户信息及用户角色信息
     *
     * @param index
     * @param pageSize
     * @return
     */
    public List<User> findUserByPage(@Param("index") Integer index ,
                                     @Param("pageSize") Integer pageSize);

    /**
     * 分页查询：统计查询总记录数
     * @return
     */
    @Select("select count(*) from t_user")
    public int findCount();

    //TODO:根据ID删除用户
    //根据用户id删除中间表的用户和角色id
    @Delete("delete from t_user_role where user_id = #{id}")
    boolean deleteRoleByUid(@Param("id") Integer uid);
    // 根据Id删除用户
    @Delete("delete from t_user where id=#{uid}")
    void deleteUserById(Integer uid);

    //TODO:修改用户
    //修改用户的基本数据（用户表）
    @Update("update t_user set username=#{username},password=#{password},email=#{email} where id = #{id}")
    boolean updateUser(AddUser addUser);

    // 添加用户的角色数据 （用户角色表）
    @Insert("insert into t_user_role(user_id, role_id) values (#{userid},#{roleid})")
    void addRoleByUser(@Param("userid") Integer uid,
                       @Param("roleid") String roleId);

    //TODO:模糊查询
    List<User> selectUserByPageAndUserName(Integer index, Integer pageSize, String search);
    @Select("select count(*) from t_user where username like '%${username}%'")
    int findCountByUserName(@Param("username") String search);
}
